/**
 * Copyright (C), 2020-2020,贵州铭明网络科技有限公司
 * FileName: PermissionsInfoServiceImpl
 * Author:   杨朝湖
 * Date:     2020/12/8 10:03
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.mm.service.impl;

import com.mm.bo.InsertPermAdminBo;
import com.mm.bo.UpdatePermAdminBo;
import com.mm.mapper.AdministratorInfoMapper;
import com.mm.mapper.LogsInfoMapper;
import com.mm.mapper.PermissionsInfoMapper;
import com.mm.mapper.PosiPermInfoMapper;
import com.mm.org.n3r.idworker.Sid;
import com.mm.pojo.*;
import com.mm.redis.RedisService;
import com.mm.rediskey.AdminKey;
import com.mm.service.PermissionsInfoService;
import com.mm.vo.*;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Gym
 * @create 2020/12/8
 * @since 1.0.0
 */
@Service
public class PermissionsInfoServiceImpl implements PermissionsInfoService {
    @Resource
    private PermissionsInfoMapper permissionsInfoMapper;
    @Resource
    private Sid sid;
    @Resource
    private RedisService redisService;
    @Resource
    private LogsInfoMapper logsInfoMapper;
    @Resource
    private AdministratorInfoMapper administratorInfoMapper;
    @Resource
    private PosiPermInfoMapper posiPermInfoMapper;

    @Override
    public List<PermMenuVo> queryMenuList() {
        List<PermMenuVo> permMenuVos = permissionsInfoMapper.queryPermMenu();
        for (int i = 0; i < permMenuVos.size(); i++) {
            if (StringUtils.isNotEmpty(permMenuVos.get(i).getPermId())) {
                List<PermMenuTwoVo> permMenuTwoVos = permissionsInfoMapper.queryMenuTwo(permMenuVos.get(i).getPermId());
                permMenuVos.get(i).setChildren(permMenuTwoVos);
                for (int j = 0; j < permMenuTwoVos.size(); j++) {
                    List<PermButtonVo> permButtonVos = permissionsInfoMapper.queryButton(permMenuTwoVos.get(j).getPermId());
                    permMenuTwoVos.get(j).setButton(permButtonVos);
                }
            }
        }
        return permMenuVos;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertPremInfo(InsertPermAdminBo insertPermAdminBo) {
        //获取当前管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, insertPermAdminBo.getToken(), AdministratorInfo.class);
        //获取所有的权限信息
        Object permList = insertPermAdminBo.getPermList();
        JSONArray jsonArray = JSONArray.fromObject(permList);
        for (Object permId : jsonArray) {
            PosiPermInfo posiPermInfo = new PosiPermInfo();
            posiPermInfo.setPpId(sid.nextShort());
            posiPermInfo.setAdminId(insertPermAdminBo.getAdminId());
            posiPermInfo.setPermId(permId.toString());
            posiPermInfoMapper.insertSelective(posiPermInfo);
        }
        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取被添加的管理员
        AdministratorInfo administratorInfoDB = administratorInfoMapper.selectByPrimaryKey(insertPermAdminBo.getAdminId());
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "添加了：" + administratorInfoDB.getAdminRealName() + "的权限信息";
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<PermAdminMenuVo> queryAdminMenuList(String token) {
        //获取登录的用户信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, token, AdministratorInfo.class);
        List<PermAdminMenuVo> permAdminMenuVos = posiPermInfoMapper.queryAdminMenu(administratorInfoRedis.getAdminId());
        for (int i = 0; i < permAdminMenuVos.size(); i++) {
            List<PermAdminMenuTwoVo> permAdminMenuTwoVos = posiPermInfoMapper.queryAdminMenuTwo(permAdminMenuVos.get(i).getPermId());
            permAdminMenuVos.get(i).setChildren(permAdminMenuTwoVos);
            for (int j = 0; j < permAdminMenuTwoVos.size(); j++) {
                List<PermAdminMenuButtonVo> permAdminMenuButtonVos = posiPermInfoMapper.queryAdminButton(permAdminMenuTwoVos.get(j).getPermId());
                permAdminMenuTwoVos.get(j).setButton(permAdminMenuButtonVos);
            }
        }

        return permAdminMenuVos;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updatePremInfo(UpdatePermAdminBo updatePermAdminBo) {
        //获取当前管理员信息
        AdministratorInfo administratorInfoRedis = redisService.get(AdminKey.admin, updatePermAdminBo.getToken(), AdministratorInfo.class);
        //查询当前管理员拥有的所有权限信息
        Example examplePermAll = new Example(PosiPermInfo.class);
        examplePermAll.createCriteria().andEqualTo("adminId", updatePermAdminBo.getAdminId());
        List<PosiPermInfo> posiPermInfos = posiPermInfoMapper.selectByExample(examplePermAll);
        //获取当前新的权限信息
        JSONArray permList = JSONArray.fromObject(updatePermAdminBo.getPermList());
        //声明变量储存相同的权限Id
        List<String> identical = new ArrayList<>();
        for (int i = 0; i < posiPermInfos.size(); i++) {
            if (permList.contains(posiPermInfos.get(i).getPermId())) {
                identical.add(posiPermInfos.get(i).getPermId());
            }
        }
        //处理不需要的权限信息
        List<String> notPermId = new ArrayList<>();
        for (int i = 0; i < posiPermInfos.size(); i++) {
            if (!permList.contains(posiPermInfos.get(i).getPermId())) {
                notPermId.add(posiPermInfos.get(i).getPermId());
            }
        }

        //处理需要的权限信息
        List<String> needPermId = new ArrayList<>();
        for (int i = 0; i < permList.size(); i++) {
            if (!identical.contains(permList.get(i).toString())) {
                needPermId.add(permList.get(i).toString());
            }
        }

        //删除不需要的权限信息
        for (int i = 0; i < notPermId.size(); i++) {
            Example example = new Example(PosiPermInfo.class);
            example.createCriteria().andEqualTo("adminId", updatePermAdminBo.getAdminId()).andEqualTo("permId", notPermId.get(i));
            posiPermInfoMapper.deleteByExample(example);
        }
        //添加需要的
        for (int i = 0; i < needPermId.size(); i++) {
            PosiPermInfo posiPermInfo = new PosiPermInfo();
            posiPermInfo.setPpId(sid.nextShort());
            posiPermInfo.setPermId(needPermId.get(i));
            posiPermInfo.setAdminId(updatePermAdminBo.getAdminId());
            posiPermInfoMapper.insertSelective(posiPermInfo);
        }

        //获取当前时间戳
        long timeMillis = System.currentTimeMillis();
        //获取被添加的管理员
        AdministratorInfo administratorInfoDB = administratorInfoMapper.selectByPrimaryKey(updatePermAdminBo.getAdminId());
        //获取日志详情
        String log_detail = "管理员：" + administratorInfoRedis.getAdminRealName() + "修改了：" + administratorInfoDB.getAdminRealName() + "的权限信息";
        LogsInfo logsInfo = new LogsInfo(sid.nextShort(), administratorInfoRedis.getAdminId(), log_detail, timeMillis, timeMillis);
        logsInfoMapper.insertSelective(logsInfo);
        return 1;
    }
}